<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\ReservationImgs;
use App\Admin\Renderable\ReservationVideo;
use App\Admin\Repositories\Reservation;
use App\Models\Reservation as ReservationModel;
use App\Models\ReservationServiceCategory;
use App\Models\SystemRegion;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Support\Helper;
use Faker\Factory;
use Dcat\Admin\Widgets\Table;

class ReservationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $service = ReservationServiceCategory::pluck('name','id');

        return Grid::make(new Reservation(), function (Grid $grid) use($service) {

            $grid->column('id')->sortable();
            $grid->column('content')->width('200px')->limit(50);

            $grid->column('file', '图片、视频')
                ->if (function () {
                    return $this->imgs;
                })
                ->display('查看')
                ->expand(function(){
                    return $this->imgs ? ReservationImgs::make(['imgs' => $this->imgs]) : '';
                })
                ->else()
                ->display(function(){
                    $html = '';
                    if ($this->video) {
                        $html = '<a href="'.config('app.img_url') . $this->video.'" target="_blank"><i class="fa fa-video-camera"></i>  查看</a>';
                    }
                    return $html;
                });

            $grid->column('service_category_id') ->display(function ($service_category_id) use($service){
                return $service ? $service[ $service_category_id ] : '';
            });

            $grid->column('expert_user_id')->display(function($expert_user_id){
                $user = User::find($expert_user_id);
                return $user ? ($user -> real_name ?: $user -> user_name) : '';
            });

            $grid->column('expert_comment_state', '专家评论') -> display(function($expert_comment_state){
                return $expert_comment_state ==1 ? '<div class="text-green">是</div>' : '<div class="text-red">否</div>';
            });

            $grid->column('region_id')->display(function($region_id){
                $systemRegion = SystemRegion::find($region_id);
                return  $systemRegion ? $systemRegion -> region_name : '';
            });

            $grid->column('is_top')->switch()->sortable();
            $grid->column('is_show')->switch();
            $grid->column('user_id')->display(function($user_id){
                $user = User::find($user_id);
                return $user ? ($user -> real_name ?: $user -> user_name) : '';
            });
            $grid->column('created_at', '发布时间');

            $grid->column('comments','评论')->display(function () {
                if ($this->comment_num > 0) {
                    return '<a href="'. admin_base_path('reservation_comment?rid='.$this->id) .'">查看</a>';
                } else {
                    return '';
                }
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $category = ReservationServiceCategory::pluck('name','id');

                $system_regions = SystemRegion::pluck('region_name','id');
                $system_regions[]  = '其他';
                $selector->selectOne('service_category_id', '服务类型', $category);
                $selector->selectOne('region_id', '地区', $system_regions);
                $selector->selectOne('expert_comment_state', '评论状态', [0 => '未评论', 1 => '已评论']);
                $selector->selectOne('is_show', '展示状态', [0 => '隐藏', 1 => '展示']);
            });

            $grid->quickSearch(['content'])->placeholder('搜索...');

            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid ->disableEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Reservation(), function (Show $show) {
            $show->field('id');
            $show->field('content');
            $show->field('description');

            $show->field('types') ->as(function($types){
                return $types == 1 ? '多图' : '视频';
            });
            $show->field('imgs') ->image();

            $show->field('video') ->image();
            $show->field('service_category_id') ->as(function($service_category_id){
                $serviceCategory = ReservationServiceCategory::find($service_category_id);
                return $serviceCategory ? $serviceCategory->name : '';
            });
            $show->field('expert_user_id') ->as(function($expert_user_id){
                $expert = User::find($expert_user_id);
                return $expert ? ($expert -> real_name ?: $expert -> user_name ) : '';
            });
            $show->field('expert_comment_state') ->as(function($expert_comment_state){
                return $expert_comment_state ? '已评论' : '未评论';
            });
            $show->field('contact');

            $show->field('region_id') ->as(function($region_id){
                $systemRegion = SystemRegion::find($region_id);
                return $systemRegion ? $systemRegion -> region_name : '';
            });
            $show->field('zan_num');
            $show->field('comment_num','评论数');
            $show->field('share_num');
            $show->field('is_top') ->as(function($is_top){
                return $is_top ? '已置顶' : '未置顶';
            });
            $show->field('weight');
            $show->field('is_show') ->as(function($is_show){
                return $is_show ? '展示' : '隐藏';
            });;
            $show->field('user_id')->as(function($user_id){
                $user = User::find($user_id);
                return $user ? ($user -> real_name ?: $user -> user_name ) : '';
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Reservation(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('content')->required();
            $form->text('description');

            $form->radio('types')->when(1,function(Form $form){

                $form->multipleImage('imgs')->move('goods/'.date('Y-m-d'))->saving(function ($paths) {
                    $paths = Helper::array($paths);
                    return json_encode($paths, true); // 最终转化为json保存到数据库
                })->customFormat(function ($paths) {
                    return Helper::array($paths); // 转为数组
                })->autoUpload();
            })->when(2,function(Form $form){

                $form->file('video')->accept('mp4')
                    ->chunkSize(5*1024*1024);
            }) ->options([1 => '多图', 2 => '视频']);





            $serviceCategoryList = ReservationServiceCategory::pluck('name','id');
            $form->select('service_category_id') ->options($serviceCategoryList) ->loads('expert_user_id','/api/expert');
            $form->select('expert_user_id');
            $form->text('contact') ->rules('required|regex:/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/',[
                'required' => '请输入联系方式',
                'regex' => '请输入正确的手机号',
            ]);

            $region = SystemRegion::pluck('region_name','id');
            $form->select('region_id') ->options($region);

            $form->radio('is_top') -> options([1 => '是', 0 => '否']) ->default(0);
            $form->number('weight');
            $form->radio('is_show') -> options([1 => '显示', 0 => '隐藏']) ->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
